package com.example;

import java.io.*;
import java.lang.reflect.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import org.apache.commons.fileupload.util.Streams;

public class Utils {
	/**
	 * 空值
	 * */
	public static Boolean isEmpty(String param) {
		return (param == null || param.equals("")) ? true : false;
	}

	/**
	 * 转码 To UTF-8
	 * */
	public static String String2UTF8(String param) {
		if (isEmpty(param))
			return param;
		try {
			return new String(param.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return param;
		}
	}

	/**
	 * 以指定的分隔符连接每一个元素
	 * */
	public static String join(ArrayList<String> list, String separator) {
		if (list == null || list.isEmpty())
			return "";
		else {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < list.size(); i++) {
				if (i < list.size() - 1)
					sb.append(list.get(i) + separator);
				else
					sb.append(list.get(i));
			}
			return sb.toString();
		}

	}

	public static void main(String[] args) throws UnsupportedEncodingException {

	}

	/**
	 * 执行指定对象的指定方法.
	 * 
	 * @param obj
	 *            指定对象.
	 * @param methodName
	 *            方法名.
	 * @param args
	 *            方法参数,没有为空
	 * @return 处理结果
	 * */
	public static Object ExceMethod(Object obj, String methodName, Object[] args) {
		Object rt = null;
		Class[] argsClazz = null;
		if (args != null && args.length != 0) {
			argsClazz = new Class[args.length];
			for (int i = 0; i < args.length; i++) {
				argsClazz[i] = args[i].getClass();
			}
		}

		try {
			Method method = obj.getClass().getMethod(methodName, argsClazz);
			rt = method.invoke(obj, args);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return rt;
	}

	/**
	 * 获取指定路径下的属性列表
	 * */
	private static Properties getProperties(String name) {
		Properties properties = new Properties();
		try {
			InputStream in = Utils.class.getClass().getResourceAsStream(name);
			properties.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}

	/**
	 * 将 url 所指向的文件下载保存到本地.
	 * 
	 * @param source
	 *            网上文件 url 链接.
	 * @param target
	 *            保存文件目录路径.
	 * @return 文件名
	 * */
	public static String downloadFile(String source, String target) {
		String name = null;
		try {
			// 解析 url, 生成新文件名.
			URL url = new URL(source);
			name = url.getFile();
			String query = url.getQuery();
			if (query != null)
				name = name.replace("?" + query, "");
			name = UUID.randomUUID().toString()
					+ name.substring(name.lastIndexOf("."));

			// 保存文件
			File file = new File(target + name);
			System.out.print(new BufferedReader(new InputStreamReader(url
					.openStream())).readLine());
			BufferedInputStream in = new BufferedInputStream(url.openStream());
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(file));
			Streams.copy(in, out, true);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return name;
	}
}
